package cn.juque.datapipeline.config;

import cn.juque.datapipeline.quartz.SpringJobFactory;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>QuartzConfig</li>
 * </ul>
 * @date 2023-04-07 15:05:33
 **/
@Configuration
public class QuartzConfig {

    @Value("${quartz.jobStore.useProperties}")
    private Boolean jobStoreUseProperties;

    @Value("${quartz.jobStore.tablePrefix}")
    private String jobStoreTablePrefix;

    @Value("${quartz.jobStore.isClustered}")
    private Boolean jobStoreIsClustered;

    @Value("${quartz.jobStore.clusterCheckinInterval}")
    private String jobStoreClusterCheckinInterval;

    @Value("${quartz.jobStore.txIsolationLevelReadCommitted}")
    private Boolean jobStoreTxIsolationLevelReadCommitted;

    @Value("${quartz.jobStore.class}")
    private String jobStoreClass;

    @Value("${quartz.jobStore.driverDelegateClass}")
    private String jobStoreDriverDelegateClass;

    @Value("${quartz.scheduler.instanceName}")
    private String schedulerInstanceName;

    @Value("${quartz.scheduler.instanceId}")
    private String schedulerInstanceId;

    @Value("${quartz.threadPool.class}")
    private String threadPoolClass;

    @Value("${quartz.threadPool.threadCount}")
    private String threadPoolThreadCount;

    @Value("${quartz.threadPool.threadPriority}")
    private String threadPoolThreadPriority;

    @Resource
    private SpringJobFactory springJobFactory;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setDataSource(dataSource);
        Properties properties = new Properties();
        properties.put("org.quartz.jobStore.useProperties", jobStoreUseProperties);
        properties.put("org.quartz.jobStore.tablePrefix", jobStoreTablePrefix);
        properties.put("org.quartz.jobStore.isClustered", jobStoreIsClustered);
        properties.put("org.quartz.jobStore.clusterCheckinInterval", jobStoreClusterCheckinInterval);
        properties.put("org.quartz.jobStore.txIsolationLevelReadCommitted", jobStoreTxIsolationLevelReadCommitted);
        properties.put("org.quartz.jobStore.class", jobStoreClass);
        properties.put("org.quartz.jobStore.driverDelegateClass", jobStoreDriverDelegateClass);
        properties.put("org.quartz.scheduler.instanceName", schedulerInstanceName);
        properties.put("org.quartz.scheduler.instanceId", schedulerInstanceId);
        properties.put("org.quartz.threadPool.class", threadPoolClass);
        properties.put("org.quartz.threadPool.threadCount", threadPoolThreadCount);
        properties.put("org.quartz.threadPool.threadPriority", threadPoolThreadPriority);
        schedulerFactoryBean.setQuartzProperties(properties);
        schedulerFactoryBean.setSchedulerName("dpp-scheduler");
        schedulerFactoryBean.setStartupDelay(1);
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContextKey");
        // QuartzScheduler 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        schedulerFactoryBean.setAutoStartup(true);
        schedulerFactoryBean.setJobFactory(this.springJobFactory);
        return schedulerFactoryBean;
    }

    @Bean
    public Scheduler scheduler(DataSource dataSource) {
        return this.schedulerFactoryBean(dataSource).getScheduler();
    }
}
